{
    "metadata": {
        "kernelspec": {
            "name": "python3",
            "display_name": "Python 3",
            "language": "python"
        },
        "language_info": {
            "name": "python",
            "version": "3.6.6",
            "mimetype": "text/x-python",
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "pygments_lexer": "ipython3",
            "nbconvert_exporter": "python",
            "file_extension": ".py"
        }
    },
    "nbformat_minor": 2,
    "nbformat": 4,
    "cells": [
        {
            "cell_type": "markdown",
            "source": [
                "Create Azure SQL Database\n",
                "============================================\n",
                "\n",
                "Steps of this procedure include:\n",
                "1. Set variables and set up Notebook\n",
                "1. Connect to Azure account and subscription\n",
                "1. Provision firewall rules\n",
                "1. Create SQL database resource"
            ],
            "metadata": {
                "azdata_cell_guid": "6af59d69-ade7-480a-b33e-52a86fe5bfd3"
            }
        },
        {
            "cell_type": "markdown",
            "source": [
                "## Set variables\r\n",
                "These variables are set based on your inputs in the deployment wizard. You can make changes to these variables but be aware of possible validation errors caused by your changes.\r\n",
                "\r\n",
                "\r\n",
                "\r\n",
                "\r\n",
                ""
            ],
            "metadata": {
                "azdata_cell_guid": "b57c46c8-4a34-49af-9b62-aa5688a02002"
            }
        },
        {
            "cell_type": "markdown",
            "source": [
                "## Notebook Setup "
            ],
            "metadata": {
                "azdata_cell_guid": "19ebf0fd-7010-4cd6-8bcd-d2f63dc75cfb"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "import sys, os, json, time, string, random, subprocess\r\n",
                "def run_command(command, json_decode = True, printOutput = True):\n",
                "    print(command)\n",
                "    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)\n",
                "    output, error = process.communicate()\n",
                "    if process.returncode != 0: \n",
                "        print(\"Process failed %d \\n%s\" % (process.returncode, error.decode(\"utf-8\")))\n",
                "        raise Exception()\n",
                "    if output:\n",
                "        output = output.decode(\"utf-8\")\n",
                "        if printOutput:\n",
                "            print(output)\n",
                "        try:\n",
                "            return json.loads(output)\n",
                "        except:\n",
                "            return output\r\n"
            ],
            "metadata": {
                "azdata_cell_guid": "c320ffe2-c488-4bd8-9886-c7deeae02996",
                "tags": []
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "## Connecting to your Azure account\r\n",
                ""
            ],
            "metadata": {
                "azdata_cell_guid": "e34334a7-0d55-4c18-8c0a-1c4a673629cd"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "subscriptions = run_command('az account list', printOutput = False)\r\n",
                "if azure_sqldb_subscription not in (subscription[\"id\"] for subscription in subscriptions):\r\n",
                "    run_command('az login')"
            ],
            "metadata": {
                "azdata_cell_guid": "96800b54-48a8-463b-886c-3d0e96f29765"
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "## Setting your Azure subscription\r\n",
                ""
            ],
            "metadata": {
                "azdata_cell_guid": "ed6b781d-ce7e-4b51-a7ec-1eeeb2032c73"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "run_command(\r\n",
                "    'az account set '\r\n",
                "    '--subscription {0}'\r\n",
                "    .format(\r\n",
                "        azure_sqldb_subscription));"
            ],
            "metadata": {
                "azdata_cell_guid": "17b57956-98cf-44de-9ab5-348469ddabf4"
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "## Create a server firewall rule\r\n",
                "\r\n",
                "This firewall rule will allow you to access your server and database within IP range immediately after it is created."
            ],
            "metadata": {
                "azdata_cell_guid": "ba895abf-3176-48b5-9e49-a060b3f74370"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "if azure_sqldb_enable_firewall_rule == True:\r\n",
                "    create_firewall_rule_result = run_command(\r\n",
                "        'az sql server firewall-rule create '\r\n",
                "        '--start-ip-address {0} '\r\n",
                "        '--end-ip-address {1} '\r\n",
                "        '--server {2} '\r\n",
                "        '--name {3} '\r\n",
                "        '--resource-group {4} '\r\n",
                "        .format(\r\n",
                "            azure_sqldb_ip_start, \r\n",
                "            azure_sqldb_ip_end, \r\n",
                "            azure_sqldb_server_name, \r\n",
                "            azure_sqldb_firewall_name, \r\n",
                "            azure_sqldb_resource_group_name));"
            ],
            "metadata": {
                "azdata_cell_guid": "ceae5670-292f-4c45-9c10-4ac85baf2d07"
            },
            "outputs": [],
            "execution_count": null
        },
        {
            "cell_type": "markdown",
            "source": [
                "## Create Azure SQL Database\n",
                "\n",
                "Learn more about the different [cost and performance options](https://go.microsoft.com/fwlink/?linkid=842067) and [other additional customizations](https://go.microsoft.com/fwlink/?linkid=2147212) for creating the database"
            ],
            "metadata": {
                "azdata_cell_guid": "b460ca8f-65a7-4d6c-94b7-6d7dd9655fad"
            }
        },
        {
            "cell_type": "code",
            "source": [
                "create_database_result = run_command(\r\n",
                "    'az sql db create '\r\n",
                "    '--server {0} '\r\n",
                "    '--name {1} '\r\n",
                "    '--edition GeneralPurpose '\r\n",
                "    '--compute-model Serverless '\r\n",
                "    '--family Gen5 '\r\n",
                "    '--resource-group {2} '\r\n",
                "    '--min-capacity 0.5 '\r\n",
                "    '--max-size 32GB '\r\n",
                "    '--capacity 1 '\r\n",
                "    '--collation {3} '\r\n",
                "    .format(\r\n",
                "        azure_sqldb_server_name, \r\n",
                "        azure_sqldb_database_name, \r\n",
                "        azure_sqldb_resource_group_name, \r\n",
                "        azure_sqldb_collation));"
            ],
            "metadata": {
                "azdata_cell_guid": "dc3b2f6f-83ac-4a4d-9d81-2f534e90913e"
            },
            "outputs": [],
            "execution_count": null
        }
    ]
}